//
//  LZPlayingViewController.m
//  LZMusic
//
//  Created by 郝庆 on 16/6/12.
//  Copyright © 2016年 haoqing. All rights reserved.
//

#import "LZPlayingViewController.h"
#import "LZMusicTool.h"
#import "LZAudioTool.h"
#import "LZMusic.h"

#define LZColor(r,g,b) ([UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0])

@interface LZPlayingViewController ()
/** 歌手背景图片 */
@property (weak, nonatomic) IBOutlet UIImageView *albumView;
/** 歌手图片 */
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
/** 歌曲名 */
@property (weak, nonatomic) IBOutlet UILabel *songLabel;
/** 歌手名 */
@property (weak, nonatomic) IBOutlet UILabel *singerLabel;
/** 当前播放时间 */
@property (weak, nonatomic) IBOutlet UILabel *currentTimeLabel;
/** 歌曲的总时间 */
@property (weak, nonatomic) IBOutlet UILabel *totalTimeLabel;
/** 进度条 */
@property (weak, nonatomic) IBOutlet UISlider *progressSlider;
/** 暂停/播放按钮 */
@property (weak, nonatomic) IBOutlet UIButton *playOrPauseBtn;

/** 进度的Timer */
@property (nonatomic, strong) NSTimer *progressTimer;

/** 歌词更新的定时器 */
@property (nonatomic, strong) CADisplayLink *lrcTimer;

/** 当前的播放器 */
@property (nonatomic, strong) AVAudioPlayer *currentPlayer;

#pragma mark - slider的事件处理
- (IBAction)startSlide;
- (IBAction)sliderValueChange;
- (IBAction)endSlide;

#pragma mark - 歌曲控制的事件处理
//- (IBAction)playOrPause;
//- (IBAction)previous;
//- (IBAction)next;

@end

@implementation LZPlayingViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.添加毛玻璃效果
    [self setupBlurView];
    
    // 2.设置滑块的图片
    [self.progressSlider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
    
    // 3.展示界面的信息
    [self displayPageInfo];
    
    [self.progressSlider addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderClick:)]];
}

#pragma mark - 设置iconView圆角
- (void)viewWillLayoutSubviews
{
    self.iconView.layer.cornerRadius = self.iconView.bounds.size.width * 0.5;
    self.iconView.layer.masksToBounds = YES;
    self.iconView.layer.borderWidth = 8.0;
    self.iconView.layer.borderColor = LZColor(36, 36, 36).CGColor;
}

#pragma mark - 添加毛玻璃效果
- (void)setupBlurView
{
    UIToolbar *toolBar = [[UIToolbar alloc] init];
    toolBar.barStyle = UIBarStyleBlack;
    toolBar.frame = [UIScreen mainScreen].bounds;
    [self.albumView addSubview:toolBar];
}

#pragma mark - 展示界面的信息
- (void)displayPageInfo
{
    // 1.取出当前播放歌曲
    LZMusic *playingMusic = [LZMusicTool playingMusic];
    
    // 2.设置界面信息
    self.albumView.image = [UIImage imageNamed:playingMusic.icon];
    self.iconView.image = [UIImage imageNamed:playingMusic.icon];
    self.songLabel.text = playingMusic.name;
    self.singerLabel.text = playingMusic.singer;
    
    // 3.开始播放歌曲
    AVAudioPlayer *currentPlayer = [LZAudioTool playMusicWithFileName:playingMusic.filename];
    self.totalTimeLabel.text = [self stringWithTime:currentPlayer.duration];
    self.currentTimeLabel.text = [self stringWithTime:currentPlayer.currentTime];
    self.currentPlayer = currentPlayer;
    self.playOrPauseBtn.selected = self.currentPlayer.isPlaying;
    
    // 4.开始播放动画
    [self startAnimateWithIconView];
    
    [self removeProgressTimer];
    [self addProgressTimer];
}

- (void)sliderClicksliderClick:(UITapGestureRecognizer *)tap
{
    // 1.获取点击的位置
    CGPoint point = [tap locationInView:tap.view];
    
    // 2.获取点击的在slider长度中占据的比例
    CGFloat ratio = point.x / self.progressSlider.bounds.size.width;
    
    // 3.改变歌曲播放的时间
    self.currentPlayer.currentTime = ratio * self.currentPlayer.duration;
    
    // 4.更新进度信息
    [self updateProgressInfo];
}

- (void)startAnimateWithIconView
{
    // 1.创建基本动画
    CABasicAnimation *rotateAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    // 2.设置基本动画属性
    rotateAnim.fromValue = @(0);
    rotateAnim.toValue = @(M_PI * 2);
    rotateAnim.repeatCount = NSUIntegerMax;
    rotateAnim.duration = 30;
    
    // 3.添加动画到图层上
    [self.iconView.layer addAnimation:rotateAnim forKey:nil];
}

#pragma mark - 对定时器的操作
- (void)addProgressTimer
{
    [self updateProgressInfo];
    self.progressTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateProgressInfo) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.progressTimer forMode:NSRunLoopCommonModes];
}

- (void)removeProgressTimer
{
    [self.progressTimer invalidate];
    self.progressTimer = nil;
}

#pragma mark - 更新进度的界面
- (void)updateProgressInfo
{
    // 1.设置当前的播放时间
    self.currentTimeLabel.text = [self stringWithTime:self.currentPlayer.currentTime];
    
    // 2.更新滑块的位置
    self.progressSlider.value = self.currentPlayer.currentTime / self.currentPlayer.duration;
}

#pragma mark - Slider的事件处理
- (IBAction)startSlide {
    [self removeProgressTimer];
}

- (IBAction)sliderValueChange {
    // 设置当前播放的时间Label
    self.currentTimeLabel.text = [self stringWithTime:self.currentPlayer.duration * self.progressSlider.value];
}

- (IBAction)endSlide {
    // 1.设置歌曲的播放时间
    self.currentPlayer.currentTime = self.progressSlider.value * self.currentPlayer.duration;
    
    // 2.添加定时器
    [self addProgressTimer];
}

- (void)sliderClick:(UITapGestureRecognizer *)sender {
    // 1.获取点击的位置
    CGPoint point = [sender locationInView:sender.view];
    
    // 2.获取点击的在slider长度中占据的比例
    CGFloat ratio = point.x / self.progressSlider.bounds.size.width;
    
    // 3.改变歌曲播放的时间
    self.currentPlayer.currentTime = ratio * self.currentPlayer.duration;
    
    // 4.更新进度信息
    [self updateProgressInfo];
}

/**
 *  设置00:00格式时间
 */
- (NSString *)stringWithTime:(NSTimeInterval)time
{
    NSInteger min = time / 60;
    NSInteger second = (NSInteger)time % 60;
    
    return [NSString stringWithFormat:@"%02ld:%02ld", min, second];
}


#pragma mark - 修改状态栏颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
@end
